//
//  ViewController.m
//  OCPropertyExport
//
//  Created by 曾少伟 on 2019/12/13.
//  Copyright © 2019 zsw. All rights reserved.
//

#import "ViewController.h"
#import "SWPropertyModel.h"
#import "TFHpple.h"
#import "FWParse.h"

@interface ViewController () <NSTextViewDelegate,NSXMLParserDelegate>
@property (weak) IBOutlet NSTextView *leftTextView;
@property (weak) IBOutlet NSTextView *rightTextView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (copy) NSString *key;
@property (copy) NSString *value;
@property (strong) SWPropertyModel *currentModel;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftTextView.delegate = self;
//    self.rightTextView.delegate = self;
    
    
    // Do any additional setup after loading the view.

}

- (void)parse {
//        NSString *path = [[NSBundle mainBundle] pathForResource:@"xml1" ofType:@"txt"];
//        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
//        NSData *data = [handle readDataToEndOfFile];
//        NSString *text = [[NSString alloc] initWithData:data encoding:(NSUTF8StringEncoding)];
    
//    NSData *data = [self.leftTextView.string dataUsingEncoding:(NSUTF8StringEncoding)];
//    TFHpple * doc = [[TFHpple alloc] initWithHTMLData:data];
//    NSArray<TFHppleElement *> * elements  = [doc searchWithXPathQuery:@"//table[@class='model']"];
//    TFHppleElement *table = elements.firstObject;
//    TFHppleElement *tbody = table.firstChild;
//    NSArray <TFHppleElement *>*propetyArr = tbody.children;
//    for (TFHppleElement * element in propetyArr) {
//        TFHppleElement *nameEle = [element searchWithXPathQuery:@"//td[@style='vertical-align: top; padding-right: 0.2em;']"].firstObject;
//        TFHppleElement *typeEle = [element searchWithXPathQuery:@"//span[@class='prop-type']"].firstObject;
//        TFHppleElement *markdownEle = [element searchWithXPathQuery:@"//div[@class='markdown']/p"].firstObject;
//        NSLog(@"%@ %@ %@",nameEle.content,typeEle.content,markdownEle.content);
//        SWPropertyModel *model = [[SWPropertyModel alloc] init];
//        model.name = nameEle.content;
//        model.markdown = markdownEle.content;
//        model.type = typeEle.content;
//        if (model.name.length > 0 && model.type.length > 0) {
//            [self.dataArr addObject:model];
//        }
//    }
    
    self.dataArr = [FWParse parseWithString:self.leftTextView.string];
    if (self.dataArr.count == 0) {
        self.rightTextView.string = @"解析个数为0";
    } else {
        [self setupRightTextViewTextWithPropertyModelArr:self.dataArr];
    }
}

- (void)setupRightTextViewTextWithPropertyModelArr:(NSArray <SWPropertyModel *>*)arr {
    NSMutableString *mStr = [[NSMutableString alloc] init];
    for (SWPropertyModel *model in arr) {
        NSString *decorateStr;
        NSString *typeStr;
        if ([model.type containsString:@"integer"]) {
            decorateStr = @"assign";
            if ([model.name hasPrefix:@"is"]) {
                typeStr = @"BOOL";
            } else {
                typeStr = @"NSInteger ";
            }
        } else if ([model.type containsString:@"number"]){
            decorateStr = @"assign";
            typeStr = @"double";
        }else if ([model.type containsString:@"bool"]){
            decorateStr = @"assign";
            typeStr = @"BOOL";
        }else {
            decorateStr = @"copy";
            typeStr = @"NSString * ";
        }
        NSString *propertyStr = [NSString stringWithFormat:@"@property (nonatomic, %@) %@", decorateStr,typeStr];
        if (propertyStr.length < 45) {
            while (propertyStr.length < 45) {
                propertyStr = [propertyStr stringByAppendingString:@" "];
            }
        }
        propertyStr = [propertyStr stringByAppendingFormat:@"%@;\n",model.name];
        propertyStr = [NSString stringWithFormat:@"/// %@\n%@",model.markdown,propertyStr];
        [mStr appendString:propertyStr];
    }
    self.rightTextView.string = mStr;
}

#pragma mark - textView delegate

- (void)textDidChange:(NSNotification *)notification {
    [self parse];
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

@end
